package wyp.lib.java.tools;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wuyiping on 14-4-9.
 */
public final class WypStringTool {
	
	private WypStringTool(){
		super();
	}

	public final static String trim(String _str) {
		if (isEmpty(_str)) {
			return "";
		}

		try {
			return _str.trim();
		} catch (Exception e) {
			return "";
		}
	}

	public final static boolean isNotEmpty(String __str) {
		if (__str == null || __str.length() < 1) {
			return false;
		}
		if ("null".equalsIgnoreCase(__str.trim()) || "undefined".equalsIgnoreCase(__str.trim())) {
			return false;
		}
		return true;
	}

	public final static boolean isEmpty(String _str) {
		return !isNotEmpty(_str);
	}

	public final static String replaceIgnoreCase(String __source, String __oldStr, String __newStr) {
		Matcher __matcher = Pattern.compile(__oldStr, Pattern.CASE_INSENSITIVE).matcher(__source);
		return __matcher.replaceAll(__newStr);
	}

	public final static String convertString4Encoding(String __source, String __oldEncoding, String __newEncoding) throws UnsupportedEncodingException {
		byte[] __bytes = __source.getBytes(__oldEncoding);
		return new String(__bytes, __newEncoding);
	}

	public final static String convertString4Encoding(String __source, String __newEncoding) throws UnsupportedEncodingException {
		return new String(__source.getBytes(), __newEncoding);
	}
}
